<style  scoped>
.demo-upload-list {
  display: inline-block;
  /* width: 60px;
  height: 60px; */
  text-align: center;
  /* line-height: 60px; */
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
<template>
  <div class="single-picture">
    <div class="demo-upload-list" :style="{height: height + 'px', width: width + 'px',lineHeight: height + 'px'}" v-for="(item,index) in uploadList" :key="index">
      <template v-if="item.status === 'finished'">
        <img :src="item.url" />
        <div class="demo-upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :show-upload-list="false"
      :on-success="handleSuccess"
      :format="['jpg','jpeg','png']"
      :max-size="2048"
      type="drag"
      :action="uploadUrl"
      :data="requestData"
      style="display: inline-block;"
      :style="{width: (width + 2) + 'px'}"
      :before-upload="handleBeforeUpload"
      v-show="!value"
    >
      <div :style="{width: width + 'px',height:height + 'px',lineHeight: height + 'px'}">
        <Icon type="ios-cloud-upload-outline" size="20"></Icon>
      </div>
    </Upload>
    <Modal :title="L('ViewImage')" :z-index="9999" :transfer=false v-model="visible">
      <img :src="imageUrl" v-if="visible" style="width: 100%" />
    </Modal>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Inject, Prop, Watch } from "vue-property-decorator";
import Util from "../lib/util";
import appconst from "../lib/appconst";
import AbpBase from "../lib/abpbase";
import Ajax from "../lib/ajax";

interface IRequestParams {
  pictureType: string;
}

@Component({
  components: {}
})
export default class SinglePicture extends AbpBase {
  @Prop({ type: String, default: "" }) value: string;

  @Prop({ type: Object }) requestData: IRequestParams;

  @Prop({
    type: Number,
    default: 60
  })
  width:number;

  @Prop({
    type: Number,
    default: 60
  })
  height:number;

  uploadList: Array<any> = new Array<any>();

  visible: boolean = false;

  uploadUrl: string = appconst.remoteServiceBaseUrl + "/Picture/UploadPicture";

  get imageUrl() {
    return appconst.remoteServiceBaseUrl + this.value;
  }

  handleView(file) {
    this.visible = true;
  }
  handleRemove(file) {
    // TODO: 删除图片
    let deleteUrl =
      appconst.remoteServiceBaseUrl +
      "/Picture/DeletePicture?PictureUrl=" +
      this.value;
    Ajax.get(deleteUrl)
      .then(() => {
        const fileList = (this.$refs.upload as any).fileList;
        (this.$refs.upload as any).fileList.splice(fileList.indexOf(file), 1);
        this.$emit("input","");
      })
      .catch(() => {
        this.$Message.error("删除图片失败");
      });
  }
  handleSuccess(res, file) {
    file.url = appconst.remoteServiceBaseUrl + res.result.url;
    //this.value = res.result.url;
    file.name = res.result.fileName;
    this.$emit("input", res.result.url);
  }
  handleBeforeUpload() {
    if (!!window.abp.auth.getToken()) {
      (this.$refs.upload as any).headers["Authorization"] =
        "Bearer " + window.abp.auth.getToken();
    }
    (this.$refs.upload as any).headers[
      ".AspNetCore.Culture"
    ] = window.abp.utils.getCookieValue("Abp.Localization.CultureName");
    (this.$refs.upload as any).headers[
      "Abp.TenantId"
    ] = window.abp.multiTenancy.getTenantIdCookie();
  }
  mounted() {
    this.uploadList = (this.$refs.upload as any).fileList;
  }
  created(){
  }
  @Watch('value')
  onValueChange(val:string,oldVal:string){
    if((val && val !=='') && this.uploadList.length==0){
      this.uploadList.push({name:'ArticleImage',url: appconst.remoteServiceBaseUrl + this.value, status: 'finished'});
      
    } else if(!val) {
      this.uploadList = [];
    }
  }
}
</script>

